﻿using Microsoft.EntityFrameworkCore;
using ZenSystemService.Api.Data;
using ZenSystemService.Api.Entities;

namespace ZenSystemService.Api.Services
{
    public class DeviceService
    {
        private readonly ApplicationDbContext _context;

        public DeviceService(ApplicationDbContext context)
        {
            _context = context;
        }

        public async Task<IEnumerable<DeviceType>> GetAllDeviceTypesAsync()
        {
            return await _context.DeviceTypes.ToListAsync();
        }

        public async Task<DeviceType> GetDeviceTypeByIdAsync(int id)
        {
            return await _context.DeviceTypes.FindAsync(id);
        }

        public bool  CreateDeviceTypeAsync(ref DeviceType deviceType)
        {
            _context.DeviceTypes.Add(deviceType);
            int count= _context.SaveChanges();
            return count > 0 ? true : false;
        }

        public async Task<bool> UpdateDeviceTypeAsync(DeviceType deviceType)
        {
            _context.Entry(deviceType).State = EntityState.Modified;
            int count = await _context.SaveChangesAsync();
            return count > 0 ? true : false;
        }

        public async Task<bool> DeleteDeviceTypeAsync(int id)
        {
            var deviceType = await _context.DeviceTypes.FindAsync(id);
            if (deviceType != null)
            {
                _context.DeviceTypes.Remove(deviceType);
                int count = await _context.SaveChangesAsync();
                return count > 0 ? true : false;
            }
            else
            { 
                return false;
            }
        }
    }
}
